import Modal from '@/components/plugin/modal/Modal.vue'
import Container from '@/components/plugin/modal/Container.vue'

export default {
  install: function (Vue, options) {
    Vue.prototype.$modal = function (text, ok) {
      let myAlert = new (Vue.extend(Modal))()
      myAlert.create(text, ok)
      document.body.appendChild(myAlert.$mount().$el)
    }

    Vue.prototype.$modal.alert = function (text, ok, title = '提示') {
      let myAlert = new (Vue.extend(Modal))()
      myAlert.create(`<p class="my-3 text-center">${text}</p>`, ok, null, title)
      document.body.appendChild(myAlert.$mount().$el)
    }

    Vue.prototype.$modal.confirm = function (text, ok, cancel = () => {}, title = '提示') {
      let myAlert = new (Vue.extend(Modal))()
      if (cancel === null) {
        cancel = () => {
        }
      }
      myAlert.create(`<p class="my-3 text-center">${text}</p>`, ok, cancel, title)
      document.body.appendChild(myAlert.$mount().$el)
    }

    Vue.prototype.$modal.container = function (component, ok, cancel = () => {}, params) {
      let myContainer = new (Vue.extend(Object.assign(Container, { store: options.store })))()
      if (cancel === null) {
        cancel = () => {
        }
      }
      myContainer.create(component, ok, cancel, params)
      document.body.appendChild(myContainer.$mount().$el)
    }
  }
}
